<?php
class Recall_main extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('recall/recall_paper_model');
		$this->load->model('recall/recall_problem_model');
		$this->load->model('recall/recall_com_model');
		$this->load->model('user/user_model');
		$this->load->helper('form');
		$this->load->library('form_validation');
		// $this->uid = $this->user_model->get_uid();
		// redirect(base_url("tipask/?user/login.html"));
	}
	
	function index($offset=0){
		//页面防刷新
		session_start();
		$allow_sep = 0.5;
		if (isset($_SESSION["post_sep"])) {
			if (time() - $_SESSION["post_sep"] < $allow_sep)  {
				show_error("请不要反复刷新");
			} else {
				$_SESSION["post_sep"] = time();
			}
		} else {
			$_SESSION["post_sep"] = time();
		}

		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {
			$data=array();
			$data['warn_flag']=1;
			if($query=$this->recall_paper_model->read_recall_paper(-1,5,$offset,'latest_alter_time','desc')){//DEBUG
				$data['all_records']=$query;
				$data['warn_flag']=0;
			}
			
			
			$this->load->library('pagination');
			
			$config['uri_segment'] = 4;
			$config['base_url'] = site_url('recall/recall_main/index');
			$config['total_rows'] = $this->recall_paper_model->return_row_cnt();
			$config['per_page'] = '10'; 
			$this->pagination->initialize($config); 

			if($query=$this->recall_paper_model->read_recall_paper(-1,5,0,'hit_cnt','desc')){//DEBUG
				$data['top_hit']=$query;
			}		
			
			$this->parser->parse('header', array('title'=>'回忆试卷'));
			$this->load->view('recall/recall_main_view',$data);
			$this->parser->parse('footer', array());
		} else redirect(base_url()."tipask/?user/login.html");
	}
	
	function add_record(){
	
		$u_id = $this->user_model->get_uid();
		
		if ($u_id != 0) {		
			if($this->input->post()){
				//表单验证
				$this->form_validation->set_rules('tags', 'Tags', 'required');
				$this->form_validation->set_rules('dscript', 'Dscript', 'required');
				$this->form_validation->set_rules('title', 'Title', 'required');
				
				if($this->form_validation->run() == TRUE){
					$data=array(
						'u_id'=>$u_id,
						'tags'=>$this->input->post('tags'),
						'dscript'=>$this->input->post('dscript'),
						'title'=>$this->input->post('title')
					);
					if($this->recall_paper_model->creat_recall_paper($data)){
						$query=$this->recall_paper_model->read_recall_paper(-1,1,0,'latest_alter_time','desc');
						$jid=$query[0]->j_id;
						redirect(site_url('recall/recall_main/show_record/'.$jid));
					}
				}else{
					show_error("输入信息不能为空");
				}
			}else{
				$this->parser->parse('header', array('title'=>'添加试卷'));
				$this->load->view('recall/recall_add_record_view');
				$this->parser->parse('footer', array());
			}
		} else redirect(base_url()."tipask/?user/login.html");
	}
	
	
	function add_problem(){
		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {	
			if($this->input->post()){
				//表单验证
				$this->form_validation->set_rules('pcontent', 'Pcontent', 'required');
				
				if($this->form_validation->run() == TRUE){
					$jid=$this->input->post('j_id');
					$data=array(
						'j_id'=>$jid,
						'u_id'=>$u_id,
						'content'=>$this->input->post('pcontent'),
					);
					$this->recall_paper_model->add_problem_cnt($jid);
					if($this->recall_problem_model->creat_recall_problem($data)) redirect(site_url('recall/recall_main/show_record/'.$jid));
				}else{
					show_error("输入信息不能为空");
				}
			}else{
				//$this->parser->parse('header', array('title'=>'添加试卷'));
				$this->load->view('recall/recall_show_record_view');
				//$this->parser->parse('footer', array());
				
			}
		} else redirect(base_url()."tipask/?user/login.html");
	}		
	
	
	function show_record($record_id=-1, $offset=0){
		//页面防刷新
		session_start();
		$allow_sep = 0.5;
		if (isset($_SESSION["post_sep"])) {
			if (time() - $_SESSION["post_sep"] < $allow_sep)  {
				show_error("请不要反复刷新");
			} else {
				$_SESSION["post_sep"] = time();
			}
		} else {
			$_SESSION["post_sep"] = time();
		}
		
		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {		
			if($record_id==-1||count($this->recall_paper_model->get_by_id($record_id))==0){
				redirect('recall/recall_main/index');
			}else{
				$this->recall_paper_model->add_hit_cnt($record_id);
				$temp=$this->recall_paper_model->get_by_id($record_id);
				$data['paper']=$temp[0];
				$data['problems']=$this->recall_problem_model->read_recall_problem($record_id,100,0,"creat_time","asc");
				for($i=0;$i<count($data['problems']);$i++){
					$tid=$data['problems'][$i]->t_id;
								
					$data['com_type0'][$i]=$this->recall_com_model->read_recall_com($tid,0,100,0,"creat_time","asc");
					
					$data['com_type1'][$i]=$this->recall_com_model->read_recall_com($tid,1,100,0,"creat_time","asc");
				}
				$this->parser->parse('header', array('title'=>'回忆 - '.$temp[0]->title));
				$this->load->view('recall/recall_show_record_view',$data);
				$this->parser->parse('footer', array());
			}
		} else redirect(base_url()."tipask/?user/login.html");
	}
		
	function modify_problem($t_record_id=-1){
		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {	
			if($t_record_id==-1||count($this->recall_problem_model->get_by_id($t_record_id))==0){
				redirect('recall/recall_main/index');
			}else{
				if($this->input->post()){
					//表单验证
					$this->form_validation->set_rules('tcontent', 'Tcontent', 'required');		
				
					if($this->form_validation->run() == TRUE){		
						$jid=$this->input->post('j_id');
						$tid=$this->input->post('t_id');
						$data=array(
							't_id'=>$this->input->post('t_id'),
							'u_id'=>$u_id,
							'content'=>$this->input->post('tcontent'),
						);
						if($this->recall_problem_model->update_recall_problem($data,$tid)) redirect(site_url('recall/recall_main/show_record/'.$jid));
					}else{
						show_error("输入信息不能为空");
					}
				}else{
					$data['problem']=$this->recall_problem_model->get_by_id($t_record_id);
					$this->load->view('recall/recall_modify_problem_view',$data);
				}
			}
		} else redirect(base_url()."tipask/?user/login.html");
	}	
		
	
	function add_com(){
		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {	
			if($this->input->post()){
				//表单验证
				$this->form_validation->set_rules('c_content', 'C_content', 'required');		
				
				if($this->form_validation->run() == TRUE){				
					$jid=$this->input->post('j_id');
					$tid=$this->input->post('t_id');
					$data=array(
						't_id'=>$tid,
						'u_id'=>$u_id,
						'type'=>$this->input->post('type'),
						'content'=>$this->input->post('c_content'),
					);
					if($this->recall_com_model->creat_recall_com($data)){
						$this->recall_com_model->add_com_cnt($tid,$this->input->post('type'));
						redirect(site_url('recall/recall_main/show_record/'.$jid));
					}
				}else{
					show_error("输入信息不能为空");
				}
			}else{
				$this->load->view('recall/recall_main/index');
			}
		} else redirect(base_url()."tipask/?user/login.html");
	}		

	
	function add_good(){
		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {	
			if($this->input->post()){
				$tid=$this->input->post('t_id');
				$jid=$this->input->post('j_id');
				if($this->recall_problem_model->add_good_cnt($tid))
					redirect(site_url('recall/recall_main/show_record/'.$jid));	
			}
		}else redirect(base_url()."tipask/?user/login.html");
	}
	
	function add_bad(){
		$u_id = $this->user_model->get_uid();
		if ($u_id != 0) {	
			if($this->input->post()){
				$tid=$this->input->post('t_id');
				$jid=$this->input->post('j_id');
				if($this->recall_problem_model->add_bad_cnt($tid))
					redirect(site_url('recall/recall_main/show_record/'.$jid));	
			}
		}else redirect(base_url()."tipask/?user/login.html");
	}
			
		
	
	
}

?>
